<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="https://unpkg.com/vue@3"></script>
</head>
<body>
    <div id="app">
        <h1>列表渲染主要通过v-for指令来实现</h1>
        <h1>知识点1：迭代普通数组</h1>
            <ul>
                <li v-for="item in times">桃花</li>
            </ul>
            <ul>
                <li v-for="item in flowers">{{item}}</li>
            </ul>
        <h1>知识点2：迭代对象数组</h1>
            <ul>
                <li v-for="(item,index) in users">{{index+1}}--{{item.id}}--{{item.name}}</li>
            </ul>
        <h1>知识点3：迭代数字</h1>
            <ul>
                <p v-for="item in 10">{{item}}</p>
            </ul>
        <h1>知识点4：关于key的属性</h1>
            <h3>为了提升循环的性能，我们一般会给每个循环加上唯一的一个key值。
                在实际中，后端会向前端返回数据时，携带唯一的与数据相关的数据条目标识符，通常是一个随机字段
            </h3>
            <p>例子</p>
            <div>
            ID：<input type="text" v-model="id"><br>
            NAME: <input type="text" v-model="name"><br>
            <button @click="add()">尾部添加</button>
            <button @click="del()">尾部删除</button>
            <button @click="add1()">头部添加</button>
            <button @click="del1()">头部删除</button>
            <table border="1">
                <tr v-for="(item,index) in users" :key="item.id">
                    <td>
                        {{item.id}}
                    </td>
                    <td>
                        {{item.name}}
                    </td>
                </tr>
            </table>
            </div><br><br><br>
        <h1>知识点5：v-for的嵌套</h1>
        <p>主要针对对象数组的迭代</p>
        <ul>
            <li v-for="(item,index) in users ":key='index'>用户{{index+1}}
                <p v-for="(list,i) in item">{{list}}</p>
            </li>
        </ul>
        </div>
</body>
</html>
<script>
    const connApp = Vue.createApp({
        data() {
            return {
                times:10,
                flowers:['西蓝花','鸡米花','菜花','爆米花','天天有钱花'],
                users:[{id:'001',name:'张三'},
                       {id:'002',name:'李四'},
                       {id:'003',name:'王五'}
                ],
                id:'',
                name:''
            }
        },
        methods: {
            add(){
                this.users.push({id:this.id,name:this.name})
            },
            del(){
                this.users.pop()
            },
            add1(){
                this.users.unshift({id:this.id,name:this.name})
            },
            del1(){
                this.users.shift()
            }
        },
    }).mount("#app")
</script>